<?php
import("ORG.Util.Function");
import("ORG.Util.Image");
import("ORG.Util.Function");
import('ORG.Net.UploadFile');
import('ORG.Net.Upload');

class RetailAction extends CommonAction {
	
	public function _initialize(){
		parent::_initialize();
	}
	//construct 
	public function __construct(){
		parent::__construct(); //must have
		$user_name          =   $_SESSION['user_name'];
		$this->assign("username",$user_name);
		$this->assign("web_title","后台管理");
	}
	
	public function _empty(){
		$this->redirect('/retail/retail_list');
	}
	
	public function retail_list(){
		$RetailModel	=	M('Retail');
		$retail_list	=	$RetailModel->select();	
		
		$this->assign("retail_list",	$retail_list);
		$this->display('retail_list');
	}
	
	public function add_retail(){		
		$_REQUEST['region_type']	=	1;
		$this->assign("action",'add_retail_ok');
		$this->display();
	}
	
	public function add_retail_ok(){		
		/*上传文件*/		
		$Upload                 =    new UploadFile();
		$Upload->maxSize        =    500000; //大小为500kb
		$Upload->allowExts      =    array('jpg', 'gif', 'png', 'jpeg');
		$ymd                    =    date('Ymd');
		$Upload->savePath       =    './Public/Upload/images/' . $ymd . '/'; //按日期目录上传文件
		$Upload->thumb          =    true;
		$Upload->thumbMaxWidth  =    '225';
		$Upload->thumbMaxHeight =    '125';
		
		$datas                  =    array();
		//判断文件
		if($Upload->upload()){
			$fileInfoList       =    $Upload->getUploadFileInfo();			
			foreach($fileInfoList as $k=>$v){
				switch($k){
					case 0:
						$datas ['orignal_img']    =    $ymd . '/' . $fileInfoList[$k]['savename'];
						$datas ['thumb_img']        =    $ymd . '/thumb_' . $fileInfoList[$k]['savename'];
					break;
					case 1:
						$datas ['other_img']       =     $ymd . '/' . $fileInfoList[$k]['savename'];
					break;
				}
			}			
		}
		
		$datas       			=     array_merge($_POST,$datas);
		$datas['description']   =     stripslashes ($datas['description']);
		$datas['ctime']   		=     time();//添加时间
		unset($datas['id']);
		/*将数据保存数据表*/
		$RetailModel			=	M('Retail');
		$datas					=	$RetailModel->create($datas);
		$id 					=	$RetailModel->add($datas);		
		if($id){
			$this->ajaxReturn('yes');
		}
		else{
			$this->ajaxReturn('no');	
		}
	}
	
	public function mod_retail(){
		$retail_id		=	$_REQUEST['id'];
		
		$sql			=	"
							select a1.*,b1.region_name province_name,b2.region_name city_name,b3.region_name district_name from ybr_retail a1 
							left JOIN ybr_region b1 on a1.province = b1.region_id
							left JOIN ybr_region b2 on a1.city = b2.region_id
							left JOIN ybr_region b3 on a1.district = b3.region_id 
							where  a1.id = '$retail_id'
							";
		
		$retail_info	=	M('Retail')->query($sql);
		$retail_info	=	$retail_info[0];
		
		$this->assign("retail_info",$retail_info);
		$this->assign("action",'mod_retail_ok');
		$this->display('mod_retail');
	}
	
	public function mod_retail_ok(){
		
		/*上传文件*/		
		$Upload                 =    new UploadFile();
		$Upload->maxSize        =    500000; //大小为500kb
		$Upload->allowExts      =    array('jpg', 'gif', 'png', 'jpeg');
		$ymd                    =    date('Ymd');
		$Upload->savePath       =    './Public/Upload/images/' . $ymd . '/'; //按日期目录上传文件
		$Upload->thumb          =    true;
		$Upload->thumbMaxWidth  =    '225';
		$Upload->thumbMaxHeight =    '125';
		
		$datas                  =    array();
		//判断文件
		if($Upload->upload()){
			$fileInfoList       =    $Upload->getUploadFileInfo();			
			foreach($fileInfoList as $k=>$v){
				switch($k){
					case 0:
						$datas ['orignal_img']    =    $ymd . '/' . $fileInfoList[$k]['savename'];
						$datas ['thumb_img']        =    $ymd . '/thumb_' . $fileInfoList[$k]['savename'];
					break;
					case 1:
						$datas ['other_img']       =     $ymd . '/' . $fileInfoList[$k]['savename'];
					break;
				}
			}			
		}
		
		$datas       			=     array_merge($_POST,$datas);
		$datas['description']   =     stripslashes ($datas['description']);
		$datas['ctime']   		=     time();//添加时间
		/*id*/
		$retail_id				=	$_REQUEST['id'];
		if(empty($_POST['city']) || empty($_POST['district'])){
			unset($datas['province']);
			unset($datas['area']);
		}
		/*将数据保存数据表*/
		$RetailModel			=	M('Retail');
		$datas					=	$RetailModel->create($datas);
		$restul					=	$RetailModel->where("id='{$retail_id}'")->save($datas);
		
		if($restul){
			$this->ajaxReturn('yes');
		}
		else{
			$this->ajaxReturn('no');	
		}
	}
	
	public function del_retail(){
		$retail_id	=	$_GET['id'];
		
		if(empty($retail_id)){
			$this->redirect("/retail/retail_list",null,2,'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">非法操作');
		}		
		M('Retail')->where("id='{$retail_id}'")->delete();
		
		$this->success('操作成功',U('retail/retail_list'));
	}
	
	public function get_region(){		
		$region_type	=	empty($_REQUEST['region_type']) ? 0 : $_REQUEST['region_type'];
		$parent_id		=	empty($_REQUEST['region_id']) ? 1 : $_REQUEST['region_id'];
		$con			=	'';
		/*构造SQL*/
		if(!empty($parent_id)){
			$con		=	'parent_id' . " ='{$parent_id}' ";
		}
		$region_list	=	M('Region')->where($con)->select();
	
		$this->assign("region_list", $region_list);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template 	= 	'Retail:l_region_list';
			$html		=	$this->fetch($template);
			$this->ajaxReturn($html);
		}
		return $region_list;
	}
	
	
}





